//
//  PanelScrollView.m
//  serviceTest
//
//  Created by glendale on 13-7-27.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "PanelScrollView.h"
#import "SLCoverView.h"
#import "UIImageView+WebCache.h"
#import "MLPhotoBrowserSignleViewController.h"
#import "UIImage+MLBrowserPhotoImageForBundle.h"

@implementation PanelScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
        self.dataArray = [NSMutableArray arrayWithCapacity:5];
//        self.dataArray = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
    }
    return self;
}

- (void)resetImg
{
    
    for (int i=0; i<self.dataArray.count; i++) {
        SLCoverView *slc = (SLCoverView *)[self.myScrollView viewWithTag:1000+i];
        NSDictionary *dic = self.dataArray[i];
        NSString *url = [NSString stringWithFormat:@"http://%@/INFO_DMS_WEB/download?FILE_ID=%@&FILE_OWNER=%@&APP_ID=AttachmentPhoto",[[NSUserDefaults standardUserDefaults]objectForKey:@"ip"],dic[@"FILE_ID"],dic[@"ENTITY_CODE"]];
        
        [slc.image sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        
        
    }
}


- (void)drawRect:(CGRect)rect
{
    // Drawing code
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024-200, 400)];
    NSInteger pag=((self.dataArray.count/6) >0 ?(self.dataArray.count/6):(1))+ ((self.dataArray.count%6) >0 ? (1):(0));
 
    self.myScrollView.contentSize = CGSizeMake((1024-200)*pag, 400);
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.delegate = self;
    [self addSubview:self.myScrollView];
    
    [self initAllRect];
    
//   NSArray *titAray= [NSArray arrayWithObjects:@"机油卡",@"机油卡",@"安心的服务保障",@"亲切贴心的服务举措",@"确实可靠的维修技术",@"迅速高效的应对能力",@"保养结算说明",@"结算说明手册",@"辅助看板", nil];
    
    //往scrollView上添加
    for (int i=0; i<self.dataArray.count; i++) {
        CGRect rect = (CGRect)[[self.rectArray objectAtIndex:i] CGRectValue];
        SLCoverView *item = [[[NSBundle mainBundle] loadNibNamed:@"SLCoverView" owner:self options:nil] objectAtIndex:0];
        [item setFrame:rect];
        //item.titlelbel.text = [self.titleArrey objectAtIndex:i];
        //item.imgName = [self.dataArray objectAtIndex:i];
        //NSLog(@" ---  %@",item.imgName);
        
        NSDictionary *dic = self.dataArray[i];
        NSString *url = [NSString stringWithFormat:@"http://%@/INFO_DMS_WEB/DelphiXmlChannel?action=DOWNLOAD&FILE_ID=%@&FILE_OWNER=%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"ip"],dic[@"FILE_ID"],dic[@"ENTITY_CODE"]];
        
        [item.image sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        
        //[item.image setImage:[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[self.dataArray objectAtIndex:i] ofType:@"jpg"]]];
        
       item.tag= 1000+i ;
        
        //添加手势
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickItem:)];
        [item addGestureRecognizer:singleTap];
        [self.myScrollView addSubview:item];
    }
}
#pragma mark - 统计所有的坐标点
/*
 操作：
 （1）统计一屏的宽度 附给 widthPerPage
 （2）统计第一屏的坐标点付给 字典 dicRectPerSec
 */

-(void)initAllRect
{
    CGFloat widthPerPage = 1024-100-100;
    NSDictionary *dicRectPerSec = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSValue valueWithCGRect:CGRectMake(5, 0, 260, 190)],@"img1",
                                   [NSValue valueWithCGRect:CGRectMake(280, 0, 260, 190)],@"img2",
                                   [NSValue valueWithCGRect:CGRectMake(555, 0, 260, 190)],@"img3",
                                   [NSValue valueWithCGRect:CGRectMake(5, 205, 260, 190)],@"img4",
                                   [NSValue valueWithCGRect:CGRectMake(280, 205, 260, 190)],@"img5",
                                   [NSValue valueWithCGRect:CGRectMake(555, 205, 260, 190)],@"img6", nil];
    
    //分配空间
    self.rectArray = [NSMutableArray arrayWithCapacity:[self.dataArray count]];

   int nPageSum =  ((self.dataArray.count/6) >0 ?(self.dataArray.count/6):(1))+ ((self.dataArray.count%6) >0 ? (1):(0));

    for (int i = 0; i<nPageSum; i++)
    {
        int nPicSum = [self.dataArray count];
        int nPicSec = (nPicSum - (i)*6) >= 6 ? (6) : (nPicSum-i*6);
        for (int j = 0; j<nPicSec; j++)
        {
            NSString *strKey = [NSString stringWithFormat:@"img%i",j+1];
            CGRect rectTmp = (CGRect)[[dicRectPerSec objectForKey:strKey]CGRectValue];
            if (0 == i)
            {
                ;
            }
            else
            {
                rectTmp.origin.x += widthPerPage;
            }
            
            int index = i*6 + j;
            [self.rectArray setObject:[NSValue valueWithCGRect:rectTmp] atIndexedSubscript:index];
        }
    }
}



-(void)onClickItem:(UITapGestureRecognizer*)gestureRecognizer{
    SLCoverView *item = (SLCoverView *)gestureRecognizer.view;
    NSDictionary *dic = self.dataArray[item.tag-1000];
    NSString *url = [NSString stringWithFormat:@"http://%@/INFO_DMS_WEB/DelphiXmlChannel?action=DOWNLOAD&FILE_ID=%@&FILE_OWNER=%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"ip"],dic[@"FILE_ID"],dic[@"ENTITY_CODE"]];
    
    
    MLPhotoBrowserSignleViewController *browserVc = [[MLPhotoBrowserSignleViewController alloc] init];
    [browserVc showHeadPortrait:item.image originUrl:url];
 
    
}

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    
        [self.webview removeFromSuperview];

}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    
    return YES;
    
}
@end
